#include "drv_tim.h"
void drv_tim7_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,ENABLE);//开TIM7时钟
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//不分频
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 10000-1;//自动重装载值修改时间为1s
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;//预分频，扩大100倍
	TIM_TimeBaseInit(TIM7, &TIM_TimeBaseInitStruct);

	TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);//定时器的更新中断

	NVIC_InitStruct.NVIC_IRQChannel = TIM7_IRQn;//通道7，定时器中断
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级，数字越小优先级越高
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;//响应优先级，数字越小优先级越高
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能中断通道
	NVIC_Init(&NVIC_InitStruct);

	TIM_Cmd(TIM7,ENABLE);
}
void drv_tim13_init(void)//beep->PF8
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//开GPIOF时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);//开TIM13时钟
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//使能上拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//速率
	GPIO_Init(GPIOF, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_TIM13);//把PF8复用为TIM13

	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//不分频
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 5000-1;//自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;//预分频
	TIM_TimeBaseInit(TIM13, &TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM13, TIM_IT_Update, ENABLE);//定时器的更新中断

	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//定时器输出模式为PWM1
	TIM_OCInitStruct.TIM_OCPolarity= TIM_OCPolarity_Low;//输出比较通道的有效极性为低电平
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//使能定时器输出比较通道的输出
	TIM_OCInitStruct.TIM_Pulse = 0;//PWM的比较值
	TIM_OC1Init(TIM13, &TIM_OCInitStruct);

	TIM_ARRPreloadConfig(TIM13, ENABLE);//使能TIM13的自动重装值预装载
	TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);//TIM13通道1的输出比较预装载
	//TIM_CtrlPWMOutputs(TIM13, ENABLE);

	TIM_Cmd(TIM13, ENABLE);
}

void drv_tim8_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//开GPIOC时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);//开TIM8时钟
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//使能上拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//速率
	GPIO_Init(GPIOC, &GPIO_InitStruct);//传参初始化GPIOC

	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);//PC6复用为TIM8

	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//定时器的时钟分频配置为不分频
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 1000-1;//定时器的自动重装值定为1ms
	TIM_TimeBaseInitStruct.TIM_Prescaler = 168-1;//预分频，
	TIM_TimeBaseInit(TIM8, &TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);//定时器的更新中断

	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//定时器输出比较模式为PWM1
	TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Reset;//定时器互补输出通道的空闲状态为低电平
	TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High;//高电平有效
	TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Enable;//互补输出通道使能
	TIM_OCInitStruct.TIM_Pulse = 1000-1;//PWM的比较值，决定PWM的占空比
	TIM_OC1Init(TIM8, &TIM_OCInitStruct);

	TIM_ARRPreloadConfig(TIM8, ENABLE);//定时器的自动重装值预装载开启，即TIM_Period的修改不会立即生效，需等到下一个更新事件才会加载新值
	TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);//TIM8通道1的输出比较预装载开启，即TIM的比较值的修改不会立即生效，需等到下一个更新事件才会加载新值
	TIM_CtrlPWMOutputs(TIM8, ENABLE);//TIM8的PWM主输出，只有高级定时器（TIM1和TIM8）是必须要开的

	TIM_Cmd(TIM8, ENABLE);
}
//PA7->TIM14_CH1
void drv_SG90_test_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//开GPIOF时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);//开TIM13时钟
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//使能上拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;//速率
	GPIO_Init(GPIOA, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM14);//把PF8复用为TIM13

	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//不分频
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 20000-1;//自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;//预分频
	TIM_TimeBaseInit(TIM14, &TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM14, TIM_IT_Update, ENABLE);//定时器的更新中断

	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//定时器输出模式为PWM1
	TIM_OCInitStruct.TIM_OCPolarity= TIM_OCPolarity_High;//输出比较通道的有效极性为低电平
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//使能定时器输出比较通道的输出
	TIM_OCInitStruct.TIM_Pulse = 0;//PWM的比较值
	TIM_OC1Init(TIM14, &TIM_OCInitStruct);

	TIM_ARRPreloadConfig(TIM14, ENABLE);//使能TIM13的自动重装值预装载
	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);//TIM13通道1的输出比较预装载
	//TIM_CtrlPWMOutputs(TIM13, ENABLE);

	TIM_Cmd(TIM14, ENABLE);
}
void SG90_SetAngle(uint16_t angle)
{
	uint16_t pulse_us;
	if(angle < 45)
	{
		angle = 0;//0.5ms
	}
	if(angle < 90)
	{
		angle = 45;//1.0ms
	}
	if(angle < 135)
	{
		angle = 90;//1.5ms
	}
	if(angle < 180)
	{
		angle = 135;//2.0ms
	}
	if(angle > 180)
	{
		angle = 180;//2.5ms
	}
	pulse_us = 500+500*(angle)/45;
	TIM_SetCompare1(TIM14,pulse_us);
}

